package HomeWork20240930;

class Clock {
    private int hour = 0;
    private int minute = 0;
    private int second = 0;

    // 设置时钟为指定的时间
    public void setTime(int hour, int minute, int second)
    {
        if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 60) {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        } else {
            System.out.println("Invalid time provided.");
        }
    }

    // 使时钟前进1秒钟的方法
    public void incSecond() {
        second++;
        if (second >= 60) {
            second = 0;
            minute++;
            if (minute >= 60) {
                minute = 0;
                hour++;
                if (hour >= 24) {
                    hour = 0;
                }
            }
        }
    }

    // 以“时：分：秒”的形式输出时钟的时间
    @Override
    public String toString() {
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }

}

class Complex {
    int realPart = 0;
    int imaginPart = 0;

    // 构造方法，初始化复数的实部和虚部
    public Complex(int r, int i) {
        this.realPart = r;
        this.imaginPart = i;
    }

    // 复数相加的方法
    public Complex complexAdd(Complex a) {
        return new Complex(this.realPart + a.realPart, this.imaginPart + a.imaginPart);
    }

    // 将复数对象转换为字符串形式
    @Override
    public String toString() {
        return realPart + "+" + imaginPart + "i";
    }
}


public class HomeWork20240930 {
    // 测试时钟类
    public static void main1(String[] args) {
        Clock clock = new Clock();
        System.out.println(clock); // 输出默认时间 00:00:00
        clock.setTime(23, 59, 58);
        System.out.println(clock); // 输出设置的时间 23:59:58
        clock.incSecond();
        System.out.println(clock); // 输出时间前进1秒后的时间 23:59:59
        clock.incSecond();
        System.out.println(clock); // 输出时间前进1秒后的时间 00:00:00
    }

    // 测试复数类
    public static void main(String[] args) {
        Complex c1 = new Complex(1, 2);
        Complex c2 = new Complex(3, 4);
        Complex sum = c1.complexAdd(c2);
        System.out.println(sum); // 输出相加后的复数 4+6i
    }
}